package com.ivr.cm.http;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class StatusController implements Controller {
	private ThreadPoolTaskExecutor taskExecutor;
    private ThreadPoolTaskExecutor dbtaskExecutor;
    private ThreadPoolTaskExecutor dispatcherTaskExecutor;

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setContentType("text/plain;charset=GBK");
		
		Runtime lRuntime = Runtime.getRuntime();
		
		StringBuilder sb = new StringBuilder();
		sb.append("<html><body>");		
		sb.append("线程池</br>");
		sb.append("<table border=1>");
		sb.append("<tr><td></td><td>当前并发线程</td><td>曾经最大并发线程</td><td>排队任务</td><td>已完成任务</td></tr>");
	    sb.append("<tr><td>主线程池</td><td>" +taskExecutor.getThreadPoolExecutor().getPoolSize()+ "</td><td>"+taskExecutor.getThreadPoolExecutor().getLargestPoolSize()+"</td><td>"+taskExecutor.getThreadPoolExecutor().getQueue().size()+"</td><td>"+taskExecutor.getThreadPoolExecutor().getCompletedTaskCount()+"</td></tr>");
	    sb.append("<tr><td>入库线程池</td><td>" +dbtaskExecutor.getThreadPoolExecutor().getPoolSize()+ "</td><td>"+dbtaskExecutor.getThreadPoolExecutor().getLargestPoolSize()+"</td><td>"+dbtaskExecutor.getThreadPoolExecutor().getQueue().size()+"</td><td>"+dbtaskExecutor.getThreadPoolExecutor().getCompletedTaskCount()+"</td></tr>");
	    sb.append("<tr><td>转发线程池</td><td>" +dispatcherTaskExecutor.getThreadPoolExecutor().getPoolSize()+ "</td><td>"+dispatcherTaskExecutor.getThreadPoolExecutor().getLargestPoolSize()+"</td><td>"+dispatcherTaskExecutor.getThreadPoolExecutor().getQueue().size()+"</td><td>"+dispatcherTaskExecutor.getThreadPoolExecutor().getCompletedTaskCount()+"</td></tr>");
	    sb.append("</table>");
		sb.append("</br>开始的内存统计信息</br>");
		sb.append("<table border=1>");
		sb.append("<tr><td></td><td>大小</td><td></td></tr>");
		sb.append("<tr><td>可用内存</td><td>" +  lRuntime.freeMemory() + "</td></tr>");
		sb.append("<tr><td>最大内存</td><td>" +  lRuntime.maxMemory() + "</td></tr>");
		sb.append("<tr><td>总内存</td><td>" +  lRuntime.totalMemory() + "</td></tr>");
		sb.append("<tr><td>可用的处理器</td><td>" +  lRuntime.availableProcessors() + "</td></tr>");
		sb.append("</table>");
		sb.append("</body></html>");
		output(response,sb.toString());
		
		return null;
	}
	
	private void output(HttpServletResponse response,String message) throws IOException{
		OutputStream os = response.getOutputStream();
		os.write(message.getBytes());
		os.flush();
		os.close();
	}

	public ThreadPoolTaskExecutor getTaskExecutor() {
		return taskExecutor;
	}

	public void setTaskExecutor(ThreadPoolTaskExecutor taskExecutor) {
		this.taskExecutor = taskExecutor;
	}

	public ThreadPoolTaskExecutor getDbtaskExecutor() {
		return dbtaskExecutor;
	}

	public void setDbtaskExecutor(ThreadPoolTaskExecutor dbtaskExecutor) {
		this.dbtaskExecutor = dbtaskExecutor;
	}

	public ThreadPoolTaskExecutor getDispatcherTaskExecutor() {
		return dispatcherTaskExecutor;
	}

	public void setDispatcherTaskExecutor(ThreadPoolTaskExecutor dispatcherTaskExecutor) {
		this.dispatcherTaskExecutor = dispatcherTaskExecutor;
	}
}